package com.briup.day42;

import java.util.*;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class Dom4jDemo {
	
	public static void main(String[] args) throws Exception {
		//1.
		SAXReader reader = new SAXReader();
		//2.
		String path = "src/com/briup/day42/classroom.xml";
		Document document = reader.read(path);
		//3. <classroom>
		Element root = document.getRootElement();
		System.out.println(root.getName()+":");
		//4.<name>
		Element nameEle = root.element("name");
		System.out.println(nameEle.getName()+":"+nameEle.getText());
		//5.<teacher>
		Element teaEle = root.element("teacher");
		System.out.println(teaEle.getName()+":");
		List<Element> childOfTeacher = teaEle.elements();
		for (Element element : childOfTeacher) {
			System.out.println(element.getName()+":"+element.getText());
		}
		//<groups>
		Element groupsEle = root.element("groups");
		List<Element> childOfGroups = groupsEle.elements();
		for (Element child : childOfGroups) {
			//id
			Element idElement = child.element("id");
			System.out.println(idElement.getName()+":"+idElement.getText());
			List<Element> stuOfGroup = child.elements("student");
			for (Element schild : stuOfGroup) {
				List<Element> stuFields = schild.elements();
				System.out.println(schild.getName()+":");
				for (Element field : stuFields) {
					System.out.println(field.getName()+":"+field.getText());
				}
			}
		}
	
	}

}
